import Vue from 'vue'
import Vuex from 'vuex'
import api from "@/api/api"
import MinCache from'@/common/util/MinCache.js'
import { ACCESS_TOKEN,USER_NAME,USER_INFO } from "@/common/util/constants"

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: '',
    userid:'',
    username: '',
    realname: '',
    welcome: '',
    avatar: ''
  },
  mutations: {
	SET_ID: (state, userid) => {
		state.userid = userid
	},
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, { username, realname, welcome }) => {
      state.username = username
      state.realname = realname
      state.welcome = welcome
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    }
  },
  actions: {
    // 登录
    mLogin({ commit }, userInfo) {
      // console.log("mLogin",userInfo)
      return new Promise((resolve, reject) => {
       api.login(userInfo).then(response => {
          if(response.data.ok){ 
            const result = response.data
			// console.log(result);
            const userInfo = result.data.userInfo;
			// console.log(userInfo);
			const token = result.data.token;
			uni.setStorageSync(ACCESS_TOKEN,token);
			uni.setStorageSync(USER_INFO,userInfo);
			commit('SET_ID', userInfo.id)
            commit('SET_TOKEN', token)
            commit('SET_AVATAR', userInfo.portrait)//头像
            commit('SET_NAME', { username: userInfo.userName,realname: userInfo.realName})
            resolve(response)
          }else{
            resolve(response)
          }
        }).catch(error => {
			console.log("catch===>response",response)
          reject(error)
        })
      })
    },
    //手机号登录
    PhoneLogin({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        api.phoneNoLogin(userInfo).then(response => {
          if(response.data.ok){
            const result = response.data
            // console.log(result);
            const userInfo = result.data.userInfo;
            // console.log(userInfo);
            const token = result.data.token;
            uni.setStorageSync(ACCESS_TOKEN,token);
            uni.setStorageSync(USER_INFO,userInfo);
			commit('SET_ID', userInfo.id)
            commit('SET_TOKEN', token)
            commit('SET_AVATAR', userInfo.portrait)//头像
            commit('SET_NAME', { username: userInfo.userName,realname: userInfo.realName})
            resolve(response)
          }else{
            reject(response)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 登出
    Logout({ commit, state }) {
	 
      return new Promise((resolve) => {
        let logoutToken = state.token;
		uni.removeStorageSync(ACCESS_TOKEN);
		uni.removeStorageSync(USER_INFO);
		commit('SET_ID', '')
        commit('SET_TOKEN', '');
		commit('SET_NAME', { username:'',realname:''});
		commit('SET_AVATAR', '');
   //      api.logout(logoutToken).then(() => {
			// console.log("api.logout");
			// resolve()
   //      }).catch(() => {
			// resolve()
   //      })
			resolve()
      })
    },

  },
  getters:{
    token: state => state.token,
	username: state => {state.username=uni.getStorageSync(USER_INFO).userName; return state.username},
	realname: state => {state.realname=uni.getStorageSync(USER_INFO).realName; return state.realname},
	avatar: state => {state.avatar=uni.getStorageSync(USER_INFO).portrait; return state.avatar},
	userid:state => {state.userid=uni.getStorageSync(USER_INFO).id; return state.userid},
  }
})
